package com.suxin.exception.bussine;

import com.alibaba.fastjson.JSON;

/**
 * @author Tang
 * @classname Response
 * @description [  ]
 * @date 2022/10/12 12:00
 */
public class Response<T> {
    private int code;
    private String message;
    private T data;

    public static Response create() {
        return new Response();
    }

    public static <T> Response<T> get() {
        return new Response();
    }

    public Response<T> success() {
        this.code = ResponseStatus.SUCCESS.getCode();
        this.message = ResponseStatus.SUCCESS.getMessage();
        return this;
    }

    public Response<T> success(T data) {
        this.code = ResponseStatus.SUCCESS.getCode();
        this.message = ResponseStatus.SUCCESS.getMessage();
        this.data = data;
        return this;
    }

    public Response<T> success(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        return this;
    }

    public Response<T> success(String message, T data) {
        this.code = ResponseStatus.SUCCESS.getCode();
        this.message = message;
        this.data = data;
        return this;
    }

    public Response<T> fail() {
        this.code = ResponseStatus.FAIL.getCode();
        this.message = ResponseStatus.FAIL.getMessage();
        return this;
    }

    public Response<T> fail(String message) {
        this.code = ResponseStatus.FAIL.getCode();
        this.message = message;
        return this;
    }

    public Response<T> fail(int code, String message) {
        this.code = code;
        this.message = message;
        return this;
    }

    public Response<T> fail(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        return this;
    }

    public Response<T> error() {
        this.code = ResponseStatus.ERROR.getCode();
        this.message = ResponseStatus.ERROR.getMessage();
        return this;
    }

    public Response<T> status(ResponseStatus status) {
        this.code = status.getCode();
        this.message = status.getMessage();
        return this;
    }

    public boolean isSuccess() {
        return this.code == ResponseStatus.SUCCESS.getCode();
    }

    public String toString() {
        return JSON.toJSONString(this);
    }

    public Response() {
        this.code = ResponseStatus.ERROR.getCode();
        this.message = ResponseStatus.ERROR.getMessage();
    }

    public int getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    public T getData() {
        return this.data;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setData(T data) {
        this.data = data;
    }


}
